//
//  MYGMedicalToolsHomeController.swift
//  MYGrow
//
//  Created by 林矗 on 2024/3/10.
//

import UIKit
enum MYGMedicalToolsType {
    case bmi
    case creatinine
    case area
    case gfr
    case has
    case walk
    case cha2
    case grace
    case timi
    case crusade
    case framingham
    
    case xiuke
    case xiyang
    case dongmai
    case xinxiong
    case xinshu
    case chaoqi
    case shenxiaoqiu
    case shenshuai
    case xueya
    
    var title: String {
        switch self {
        case .bmi:
            "BMI计算工具"
        case .creatinine:
            "肌酐清除率计算工具"
        case .area:
            "体表面积计算工具"
        case .gfr:
            "GFR (CKD-EPI)"
        case .has:
            "HAS-BLED评分"
        case .walk:
            "6分钟步行测试距离"
        case .cha2:
            "CHA2DS2VASC 评分"
        case .grace:
            "GRACE评分"
        case .timi:
            "TIMI评分"
        case .crusade:
            "CRUSADE出血风险评分表"
        case .framingham:
            "Framingham风险评估"
        case .xiuke:
            "休克指数"
        case .xiyang:
            "吸氧浓度"
        case .dongmai:
            "平均动脉压"
        case .xinxiong:
            "心胸比例"
        case .xinshu:
            "心输出量"
        case .chaoqi:
            "潮气量"
        case .shenxiaoqiu:
            "肾小球滤过率"
        case .shenshuai:
            "肾衰指数"
        case .xueya:
            "血压指数"
        }
    }
    
    var themeColor: UIColor {
        switch self {
        case .bmi:
            UIColor.rgba(297, 190, 103)
        case .creatinine:
            UIColor.rgba(240, 149, 54)
        case .area:
            UIColor.rgba(117, 221, 248)
        case .gfr:
            UIColor.rgba(132, 33, 244)
        case .has:
            UIColor.rgba(237, 248, 223)
        case .walk:
            UIColor.rgba(195, 123, 130)
        case .cha2:
            UIColor.rgba(24, 63, 176)
        case .grace:
            UIColor.rgba(77, 119, 36)
        case .timi:
            UIColor.rgba(164, 35, 21)
        case .crusade:
            UIColor.rgba(80, 90, 22)
        case .framingham:
            UIColor.rgba(189, 20, 30)
        case .xiuke:
            UIColor.rgba(297, 190, 103)
        case .xiyang:
            UIColor.rgba(240, 149, 54)
        case .dongmai:
            UIColor.rgba(117, 221, 248)
        case .xinxiong:
            UIColor.rgba(132, 33, 244)
        case .xinshu:
            UIColor.rgba(237, 248, 223)
        case .chaoqi:
            UIColor.rgba(195, 123, 130)
        case .shenxiaoqiu:
            UIColor.rgba(80, 90, 22)
        case .shenshuai:
            UIColor.rgba(77, 119, 36)
        case .xueya:
            UIColor.rgba(164, 35, 21)
        }
    }
    
    var action:() -> () {
        switch self {
        case .bmi:
            return {
                let vc = MYGMedicalToolsBMIController()
                vc.title = title
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        case .creatinine:
            return {
                let vc = MYGMedicalToolsCreatinineController()
                vc.title = title
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        case .area:
            return {
                let vc = MYGMedicalToolsAreaController()
                vc.title = title
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        case .gfr:
            return {
                let vc = MYGMedicalToolsGFRController()
                vc.title = title
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        case .has:
            return {
                let vc = MYGMedicalToolsHasController()
                vc.title = title
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        case .walk:
            return {
                let vc = MYGMedicalToolsWalkController()
                vc.title = title
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        case .cha2:
            return {
                let vc = MYGMedicalToolsCHAController()
                vc.title = title
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        case .grace:
            return {
                let vc = MYGMedicalToolsGRACEController()
                vc.title = title
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        case .timi:
            return {
                let vc = MYGMedicalToolsTIMIController()
                vc.title = title
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        case .crusade:
            return {
                let vc = MYGMedicalToolsCRUSADEController()
                vc.title = title
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        case .framingham:
            return {
                let vc = MYGMedicalToolsFraminghamController()
                vc.title = title
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        case .xiuke:
            return {
                let vc = MYGProtocolController(titeleName: title)
                vc.urlString = "http://h5-dev.medataplus.com/#/pages/medicalTools/calculates?type=1"
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        case .xiyang:
            return {
                let vc = MYGProtocolController(titeleName: title)
                vc.urlString = "http://h5-dev.medataplus.com/#/pages/medicalTools/calculates?type=2"
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        case .dongmai:
            return {
                let vc = MYGProtocolController(titeleName: title)
                vc.urlString = "http://h5-dev.medataplus.com/#/pages/medicalTools/calculates?type=3"
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        case .xinxiong:
            return {
                let vc = MYGProtocolController(titeleName: title)
                vc.urlString = "http://h5-dev.medataplus.com/#/pages/medicalTools/calculates?type=4"
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        case .xinshu:
            return {
                let vc = MYGProtocolController(titeleName: title)
                vc.urlString = "http://h5-dev.medataplus.com/#/pages/medicalTools/calculates?type=5"
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        case .chaoqi:
            return {
                let vc = MYGProtocolController(titeleName: title)
                vc.urlString = "http://h5-dev.medataplus.com/#/pages/medicalTools/calculates?type=6"
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        case .shenxiaoqiu:
            return {
                let vc = MYGProtocolController(titeleName: title)
                vc.urlString = "http://h5-dev.medataplus.com/#/pages/medicalTools/calculates?type=7"
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        case .shenshuai:
            return {
                let vc = MYGProtocolController(titeleName: title)
                vc.urlString = "http://h5-dev.medataplus.com/#/pages/medicalTools/calculates?type=8"
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        case .xueya:
            return {
                let vc = MYGProtocolController(titeleName: title)
                vc.urlString = "http://h5-dev.medataplus.com/#/pages/medicalTools/calculates?type=9"
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
            }
        }
    }
}

class MYGMedicalToolsHomeController: MYGBaseController {
    // MARK: - Property
    private var tools: [MYGMedicalToolsType] = []
    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        configUI()
        configItem()
    }
    
    // MARK: - UI
    private func configUI() {
        title = "医学公式"
        view.backgroundColor = .rgba(249, 250, 252)
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.right.bottom.equalToSuperview()
        }
    }
    
    private func configItem() {
        tools = [.bmi, .creatinine, .area, .gfr, .has, .walk, .cha2, .grace, .timi, .crusade, .xiuke,
            .xiyang,
            .dongmai,
            .xinxiong,
            .xinshu,
            .chaoqi,
            .shenxiaoqiu,
            .shenshuai,
            .xueya]
        collectionView.reloadData()
    }
    
    // MARK: - lazy load
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(cellWithClass: MYGMedicalToolsHomeCell.self)
        collectionView.backgroundColor = .clear
        return collectionView
    }()
}

extension MYGMedicalToolsHomeController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        tools.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withClass: MYGMedicalToolsHomeCell.self, for: indexPath)
        cell.type = tools[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        tools[indexPath.row].action()
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        CGSize(width: (screenWidth - 45) / 2.0, height: 70)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        15
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        15
    }
}
